/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.control;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.sys.frm.BaseController;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.UuidUtil;
import com.rf.business.model.Shuttle;
import com.rf.business.service.ShuttleService;

@Controller
@RequestMapping("/bus/shuttle")
public class ShuttleController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(ShuttleController.class);
	private static final long serialVersionUID = -120161117110201268L;
	
	@Resource
	private ShuttleService shuttleService;
	
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/business/shuttle/list");
		return modelAndView; 
    }
    
    /**
	 * 搜索页面信息
	 * @param Page,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (PageUtils pageUtils, Shuttle shuttle, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	//获取分页、查询参数，进行数据封装
    	Map searchMap = shuttleService.getShuttlePage(shuttle, pageUtils);
    	super.responsePrint(request, response, JsonUtils.mapToJson(searchMap));
    }
    
    /**
     * 编辑页面初始化
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    public ModelAndView edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	ModelAndView modelAndView = new ModelAndView();
		String id = request.getParameter("id");
		Shuttle shuttle = new Shuttle();
		if(id != null && !id.equals("")){
			shuttle = shuttleService.getShuttleByID(id);	
		}else{
//			shuttle.setState(ConfigUtils.ENABLESTATE);
		}
		modelAndView.addObject("shuttle", shuttle);
        modelAndView.setViewName("/business/shuttle/edit");
        return modelAndView; 
    }
    
    /**
     * 保存数据信息
     * @param Shuttle,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    public void save (Shuttle shuttle, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String ret = "success";
    	Session session = SecurityUtils.getSubject().getSession();
    	if(shuttle !=null && shuttle.getId()!=null && !shuttle.getId().equals("")){
			//修改
			shuttleService.updateShuttle(shuttle);
		}else{
			//新增
			shuttle.setId(UuidUtil.get32UUID());
			shuttle.setCreateTime(new Date());
			shuttle.setCreateUser((String)session.getAttribute("userSessionId"));
			
			shuttleService.addShuttle(shuttle);
		}
		super.resPrint(request, response, ret);//成功 
    }
    
    /**
     * 删除数据信息
     * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete (HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	String ids = request.getParameter("ids");
    	String [] idsArr = ids.split(",");
    	for(int i=0;i<idsArr.length;i++){
    		shuttleService.deleteShuttle(idsArr[i]);
    	}
    	
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 更新数据状态
     * @param shuttle,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "updateState")
    @ResponseBody
    public void updateState (Shuttle shuttle, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	shuttleService.updateShuttle(shuttle);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
}


